/*
 * Copyright (c) 2004-2022, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.hisp.dhis.webapi.controller.event;

import com.google.common.collect.Lists;
import java.util.List;
import java.util.Map;
import org.hisp.dhis.common.DhisApiVersion;
import org.hisp.dhis.common.DimensionalItemObject;
import org.hisp.dhis.common.OpenApi;
import org.hisp.dhis.common.Pager;
import org.hisp.dhis.common.PagerUtils;
import org.hisp.dhis.dxf2.common.OrderParams;
import org.hisp.dhis.fieldfilter.FieldFilterParams;
import org.hisp.dhis.fieldfilter.FieldFilterService;
import org.hisp.dhis.fieldfiltering.FieldPreset;
import org.hisp.dhis.node.NodeUtils;
import org.hisp.dhis.node.types.RootNode;
import org.hisp.dhis.program.ProgramDataElementDimensionItem;
import org.hisp.dhis.program.ProgramService;
import org.hisp.dhis.query.Order;
import org.hisp.dhis.query.Query;
import org.hisp.dhis.query.QueryParserException;
import org.hisp.dhis.query.QueryService;
import org.hisp.dhis.schema.Schema;
import org.hisp.dhis.schema.SchemaService;
import org.hisp.dhis.webapi.mvc.annotation.ApiVersion;
import org.hisp.dhis.webapi.service.ContextService;
import org.hisp.dhis.webapi.utils.PaginationUtils;
import org.hisp.dhis.webapi.webdomain.WebMetadata;
import org.hisp.dhis.webapi.webdomain.WebOptions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Lars Helge Overland
 */
@OpenApi.Document(domain = DimensionalItemObject.class)
@Controller
@RequestMapping("/api/programDataElements")
@ApiVersion({DhisApiVersion.DEFAULT, DhisApiVersion.ALL})
public class ProgramDataElementController {
  private final QueryService queryService;

  private final FieldFilterService fieldFilterService;

  private final ContextService contextService;

  private final SchemaService schemaService;

  private final ProgramService programService;

  public ProgramDataElementController(
      QueryService queryService,
      FieldFilterService fieldFilterService,
      ContextService contextService,
      SchemaService schemaService,
      ProgramService programService) {
    this.queryService = queryService;
    this.fieldFilterService = fieldFilterService;
    this.contextService = contextService;
    this.schemaService = schemaService;
    this.programService = programService;
  }

  @GetMapping
  @SuppressWarnings("unchecked")
  public @ResponseBody RootNode getObjectList(
      @RequestParam Map<String, String> rpParameters, OrderParams orderParams)
      throws QueryParserException {
    Schema schema = schemaService.getDynamicSchema(ProgramDataElementDimensionItem.class);

    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
    List<Order> orders = orderParams.getOrders(schema);

    if (fields.isEmpty()) {
      fields.addAll(FieldPreset.ALL.getFields());
    }

    WebOptions options = new WebOptions(rpParameters);
    WebMetadata metadata = new WebMetadata();

    List<ProgramDataElementDimensionItem> programDataElements;
    Query query =
        queryService.getQueryFromUrl(
            ProgramDataElementDimensionItem.class,
            filters,
            orders,
            PaginationUtils.getPaginationData(options),
            options.getRootJunction());
    query.setDefaultOrder();

    if (options.contains("program")) {
      String programUid = options.get("program");
      programDataElements = programService.getGeneratedProgramDataElements(programUid);
      query.setObjects(programDataElements);
    }

    programDataElements = (List<ProgramDataElementDimensionItem>) queryService.query(query);

    Pager pager = metadata.getPager();

    if (options.hasPaging() && pager == null) {
      pager = new Pager(options.getPage(), programDataElements.size(), options.getPageSize());
      programDataElements = PagerUtils.pageCollection(programDataElements, pager);
    }

    RootNode rootNode = NodeUtils.createMetadata();

    if (pager != null) {
      rootNode.addChild(NodeUtils.createPager(pager));
    }

    rootNode.addChild(
        fieldFilterService.toCollectionNode(
            ProgramDataElementDimensionItem.class,
            new FieldFilterParams(programDataElements, fields)));

    return rootNode;
  }
}
